﻿using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    class TwoPagesToOnePage
    {
        void run()
        {

            // Get a fresh copy of the sample PDF file
            string filename = "sample3.pdf";

            // Create the output document
            PdfDocument outputDocument = new PdfDocument();
            // Show single pages
            // (Note: one page contains two pages from the source document)
            outputDocument.PageLayout = PdfPageLayout.SinglePage;
            XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Center;
            format.LineAlignment = XLineAlignment.Far;
            XGraphics gfx;
            XRect box;

            // Open the external document as XPdfForm object
            XPdfForm form = XPdfForm.FromFile(filename);
            for (int i = 0; i < form.PageCount; i += 2)
            {
                form.Page.MediaBox = form.Page.CropBox;
                form.PageIndex = i;
            }
            form.PageIndex = 0;
            var bleed = 5.6693;
            XRect boxSrc = new XRect(bleed, bleed, form.PointWidth - bleed - bleed, form.PointHeight - bleed - bleed);
            for (int idx = 0; idx < form.PageCount; idx++)
            {
                // Add a new page to the output document
                PdfPage page = outputDocument.AddPage();

                page.Orientation = PageOrientation.Portrait;
                page.Width = form.PointWidth * 2;
                page.Height = form.PointHeight;
                double width = page.Width;
                double height = page.Height;

                int rotate = page.Elements.GetInteger("/Rotate");

                gfx = XGraphics.FromPdfPage(page);

                // Set page number (which is one-based)
                form.PageNumber = idx + 1;


                box = new XRect(0, 0, form.PointWidth, form.PointHeight);
                // Draw the page identified by the page number like an image

                //gfx.RotateTransform(0);
                //PdfFormXObject form123 = new PdfFormXObject(outputDocument, outputDocument, page);
                //form.Page.MediaBox = form.Page.CropBox;
                gfx.DrawImage(form, box);//, boxSrc, XGraphicsUnit.Point);
                // Write document file name and page number on each page
                //box.Inflate(0, -10);
                //gfx.DrawString(String.Format("+ {1} -", filename, idx + 1),
                //  font, XBrushes.Red, box, format);

                if (idx + 1 < form.PageCount)
                {
                    // Set page number (which is one-based)
                    form.PageNumber = idx + 2;

                    box = new XRect(form.PointWidth, 0, form.PointWidth, form.PointHeight);
                    // Draw the page identified by the page number like an image
                    //form.Page.MediaBox = form.Page.CropBox;
                    //gfx.RotateTransform(45);
                    form.Page.Rotate = 180;
                    gfx.DrawImage(form, box, boxSrc, XGraphicsUnit.Point);

                    // Write document file name and page number on each page
                    //box.Inflate(0, -10);
                    //gfx.DrawString(String.Format("- {1} +", filename, idx + 2),
                    //  font, XBrushes.Red, box, format);
                }
            }

            // Save the document...
            filename = "sample3_result.pdf";
            outputDocument.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }
    }
}
